<template>
    <div class="login-page">
        <div class="container">
            <h2>登录</h2>
            <el-input v-model="username" placeholder="请输入账号" clearable />
            <el-input v-model="password" placeholder="请输入密码" clearable type="password" show-password />
            <el-button class="submit-btn" size="large" type="primary" @click="submit">登录</el-button>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const router = useRouter();
const username = ref("");
const password = ref("");

const submit = async () => {
    console.log(username.value, password.value);

    if (username.value.trim().length === 0 || password.value.trim().length === 0) {
        ElMessage.error('账号或密码不能为空');
        return;
    }

    const params = {
        username: username.value,
        password: password.value
    };

    const { data: { msg, result } } = await axios.post('/admin_login', params);

    if (msg === 'success') {
        ElMessage.success('登录成功');
        localStorage.setItem('userInfo', JSON.stringify(result));
        router.push('/repair');
    } else if (msg === 'accountError') {
        ElMessage.error('账号或密码错误');
    } else {
        ElMessage.error('服务端异常~');
    }
}

</script>

<style scoped>
.login-page {
    width: 100vw;
    height: 100vh;
    background-image: url('../assets/login-bg.png');
    position: relative;
}

.container {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 20px;
    padding: 40px;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 500px;
    gap: 20px 0;
}

.submit-btn {
    width: 100px;
}
</style>